package com.xc.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.common.Result;
import com.xc.demo.entity.ProductCategory;
import com.xc.demo.entity.dto.ProductCateChildrenDTO;
import com.xc.demo.service.ProductCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productCat")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;


//    保存或增加
    @PostMapping
    public Boolean saveCatgory(@RequestBody ProductCategory productCategory){

      return   productCategoryService.saveOrUpdate(productCategory);

    }

    @DeleteMapping("/{id}")
    public Boolean deleteCat(@PathVariable Integer id){
        return productCategoryService.removeById(id);
    }


    @GetMapping("/{id}")
    public ProductCategory getById(@PathVariable Integer id){
        ProductCategory productCategory = productCategoryService.getById(id);
        return  productCategory;
    }




 //    查询展示分页
    @GetMapping("/page")
    public IPage<ProductCategory> findPage(
            @RequestParam Long parentId,
            @RequestParam Integer pageNum,
             @RequestParam Integer pageSize

    ){
        log.info("pageNum={},pageSize={},parentId={}",pageNum,pageSize,parentId);

        IPage<ProductCategory> page= new Page<>(pageNum,pageSize);

        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("parent_id",parentId);

        queryWrapper.orderByAsc("id");

        return productCategoryService.page(page,queryWrapper);
    }

//    更新导航栏状态
    @GetMapping("/update/navStatus")
    public Result updateNavStatus(
            @RequestParam Long id,
            @RequestParam Integer navStatus
    ){

        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();

        updateWrapper.lambda().set(ProductCategory::getNavStatus,navStatus).in(ProductCategory::getId,id);

        return   Result.success(productCategoryService.update(updateWrapper));
    }

//    更新是否显示
    @GetMapping("/update/showStatus")
    public Result updateShowStatus(
            @RequestParam Long id,
            @RequestParam Integer showStatus
    ){

        UpdateWrapper<ProductCategory> updateWrapper2 = new UpdateWrapper<>();

        updateWrapper2.lambda().set(ProductCategory::getShowStatus,showStatus).in(ProductCategory::getId,id);

        return Result.success(productCategoryService.update(updateWrapper2));

    }


//    获取商品的一级分类和二级分类的级联数据

    @GetMapping("/list/withChildren")
    public Result getWithChildren(){
        List<ProductCateChildrenDTO> list = productCategoryService.getWithChildren();

        return Result.success(list);
    }



}
